package quq.missq.views;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import quq.missq.Constants;
import quq.missq.R;
import quq.missq.beans.BigShootDetailBean;
import quq.missq.beans.TopicDetailBean;
import quq.missq.beans.UserBean;
import quq.missq.utils.ShareConfig;
import quq.missq.utils.UserTools;

/**
 * Created by wcx on 2015/9/22.
 */
public class ShareDialog extends Dialog implements View.OnClickListener, PlatformActionListener {
    private HashMap<String, Object> map;

    public ShareDialog(Context context) {
        this(context, R.style.ShareDialog);
    }

    public ShareDialog(Context context, int theme) {
        super(context, theme);
        setContentView(R.layout.dialog_share);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        map = new HashMap<>();
        setDialogSize();
        init();
    }

    //初始化要分享的内容
    String share_title="";
    String share_text="";
    String share_image="";
    String share_url="";

    private BigShootDetailBean.Data data1;
    private TopicDetailBean.Data data2;
    private int type;
    public void setData_1(BigShootDetailBean.Data data){
        this.data1=data;
    }
    public void setData_2(TopicDetailBean.Data data){
        this.data2=data;
    }
    public void setType(int type){
        this.type=type;
    }

    private void init() {
        LinearLayout qq = (LinearLayout) findViewById(R.id.ll_qq);
        LinearLayout weixinfriend = (LinearLayout) findViewById(R.id.ll_weixinfriend);
        LinearLayout weixinmoments = (LinearLayout) findViewById(R.id.ll_weixinmoments);
        LinearLayout qzone = (LinearLayout) findViewById(R.id.ll_qzone);
        qq.setOnClickListener(this);
        weixinfriend.setOnClickListener(this);
        weixinmoments.setOnClickListener(this);
        qzone.setOnClickListener(this);
        findViewById(R.id.rl_cancel).setOnClickListener(this);

        UserBean.User user = UserTools.getUser(getContext());
        String avatar80 =Constants.IMAGE_HOST_Share+user.getAvatar80();
        String nkname = user.getNkname();
        share_image=avatar80;
        //share_title=nkname+"邀请你加入";
        //share_title=nkname+"《"+"邀请你加入"+"》";
        if(type==1){
            share_title="《"+data1.getTitle()+"》";
//            share_text=data1.getTitle();
            share_text=data1.getSummary();
            share_url= Constants.IMAGE_HOST+"share/view/"+data1.getId();
        }else{
            //share_text=data2.getTitle();
            share_title="分享了一个话题";
            share_text=data2.getTitle();
            //share_text=data2.getContent();
            share_url= Constants.IMAGE_HOST+"share/view/"+data2.getId();
        }
    }

    public void setDialogSize(){
        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.mystyle);
        Display play = window.getWindowManager().getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        play.getMetrics(metrics);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width=params.MATCH_PARENT;
        params.height=params.WRAP_CONTENT;
        onWindowAttributesChanged(params);
        window.setAttributes(params);
    }

    @Override
    public void onClick(View v) {
            if (map.size() != 0) {
                map.clear();
            }
            switch (v.getId()){
                case R.id.ll_qq:
                    share_QQFriend();
                    break;
                case R.id.ll_weixinfriend:
                    share_WxFriend();
                    break;
                case R.id.ll_weixinmoments:
                    share_CircleFriend();
                    break;
                case R.id.ll_qzone:
                    share_Qzone();
                    break;
                case R.id.rl_cancel:
                    dismiss();
                    break;
            }
    }

    private void share_CircleFriend() {//微信朋友圈
        Platform circle = ShareSDK.getPlatform(getContext(), WechatMoments.NAME);
        map.put("AppId", ShareConfig.APPID_CIRCLE_FRIEND);
        map.put("AppSecret", ShareConfig.APPSECRET_CIRCLE_FRIEND);
        map.put("Enable", ShareConfig.ENABLE_CIRCLE_FRIEND);
        map.put("BypassApproval", ShareConfig.BYPASSAPPROVAL_CIRCLE_FRIEND);

        ShareSDK.setPlatformDevInfo(WechatMoments.NAME, map);
        ShareSDK.initSDK(getContext(), ShareConfig.APPKEY);

        cn.sharesdk.wechat.moments.WechatMoments.ShareParams sp = new cn.sharesdk.wechat.moments.WechatMoments.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);// 一定要设置分享属性
        sp.setTitle(share_title);
        sp.setText(share_text);
        sp.setImageUrl(share_image);
        sp.setUrl(share_url);

        circle.setPlatformActionListener(this); // 设置分享事件回调
        circle.share(sp);
    }

    private void share_WxFriend() {//微信好友
        Platform circle = ShareSDK.getPlatform(getContext(), Wechat.NAME);//设置指定平台
        map.put("AppId", ShareConfig.APPID_WXFRIEND);
        map.put("Enable", ShareConfig.ENABLE_WXFRIEND);
        map.put("BypassApproval", ShareConfig.BYPASSAPPROVAL_WXFRIEND);

        ShareSDK.setPlatformDevInfo(Wechat.NAME, map);
        ShareSDK.initSDK(getContext(), ShareConfig.APPKEY);

        Wechat.ShareParams sp = new Wechat.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);
        sp.setTitle(share_title);
        sp.setText(share_text);
        sp.setImageUrl(share_image);
        sp.setUrl(share_url);
        circle.setPlatformActionListener(this);
        circle.share(sp);
    }


    private void share_QQFriend() {//qq好友
        map.put("AppId", ShareConfig.APPID_QQFRIEND);
        map.put("AppKey", ShareConfig.APPKEY_QQFRIEND);
        map.put("Enable", ShareConfig.ENABLE_QQFRIEND);

        ShareSDK.setPlatformDevInfo(QQ.NAME, map);
        ShareSDK.initSDK(getContext(), ShareConfig.APPKEY);

        cn.sharesdk.tencent.qq.QQ.ShareParams sp = new cn.sharesdk.tencent.qq.QQ.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);

        sp.setTitle(share_title);
        sp.setText(share_text);
        sp.setTitleUrl(share_url);
        sp.setImageUrl(share_image);

        Platform qq= ShareSDK.getPlatform(getContext(), QQ.NAME);
        qq.setPlatformActionListener(this);
        qq.share(sp);
    }

    private void share_Qzone() { //分享到qq空间
        map.put("AppId", ShareConfig.APPID_QZONE);
        map.put("AppKey", ShareConfig.APPKEY_QZONE);
        map.put("ShareByAppClient", ShareConfig.SHAREBYAPPCLIENT_QZONE);
        map.put("Enable", ShareConfig.ENABLE_QZONE);
        map.put("BypassApproval", ShareConfig.BYPASSAPPROVAL_QZONE);

        ShareSDK.setPlatformDevInfo(QZone.NAME, map);
        ShareSDK.initSDK(getContext(), ShareConfig.APPKEY);

        cn.sharesdk.tencent.qzone.QZone.ShareParams sp = new cn.sharesdk.tencent.qzone.QZone.ShareParams();
        sp.setShareType(Platform.SHARE_WEBPAGE);

        sp.setTitle(share_title);
        sp.setText(share_text);
        sp.setTitleUrl(share_url);//标题的超链接
        sp.setSite("MissQ");//分享此内容的网站名称,仅仅QQ空间
        sp.setSiteUrl("");//此内容的网站地址，仅在QQ空间使用
        sp.setImageUrl(share_image);
        Platform qzone = ShareSDK.getPlatform(getContext(), QZone.NAME);
        qzone.setPlatformActionListener(this);
        qzone.share(sp);
    }


    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        Toast.makeText(getContext(),"分享成功",Toast.LENGTH_LONG).show();
        dismiss();
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        Toast.makeText(getContext(),"分享失败",Toast.LENGTH_LONG).show(                    );
    }

    @Override
    public void onCancel(Platform platform, int i) {
        Toast.makeText(getContext(),"取消分享",Toast.LENGTH_LONG).show(              );
    }
}
